home *** CD-ROM | disk | FTP | other *** search
- /*****
- *
- * Grant's CGI Shell (Common Grant Interface :-)
- *
- * MyCGIProcess.c
- *
- * Sample application using the cgi interface.
- *
- * CustomCGIProcess is where you will do your application specific processing
- * of the cgi stuff.
- *
- * by Grant Neufeld
- *
- * Copyright ©1995,1996 by Grant Neufeld
- *
- * http://arpp.carleton.ca/cgi/framework/
- * gneufeld@ccs.carleton.ca
- * grant@acm.org
- *
- * The copyright notice must be kept in this source.
- * I ask that you let me know of any enhancements (read: bug fixes) to this code.
- * I would also like copies of (or discounts on) anything you produce using this code, please.
- *
- *****/
-
- #include "MyConfiguration.h"
- #if kCompileWithCGICode
-
- #include <string.h>
- #include <Threads.h>
-
- #include "globals.h"
-
- #include "CGI.h"
- #include "CustomHandlers.h"
- #include "DebugUtil.h"
- #include "LogUtil.h"
- #include "MemoryUtil.h"
- #include "ProcessUtil.h"
-
-
- /*** CUSTOM CGI FUNCTION ***/
-
- /* This function is where the CGI is actually processed.
- You should replace its contents with your own.
- You need to allocate (*theCGIHdl)->responseData using CGINewHandle.
- Put an HTTP header at the beginning of (*theCGIHdl)->responseData.
- Put your data immediately following the last character of the HTTP header.
- (*theCGIHdl)->responseData will be automatically deallocated by the CGI handler.
- You should set (*theCGIHdl)->responseSize to be the size of the responseData,
- including null terminator if you have one.
-
- (this function's prototype is defined in CustomHandlers.h) */
- void
- CustomCGIProcess ( CGIHdl theCGIHdl )
- {
- char * myResponseData;
-
- /* debugging check to ensure that theCGIHdl is not NULL */
- my_assert ( theCGIHdl != NULL,
- "\pCustomCGIProcess: theCGIHdl is NULL" );
-
- #define kHelloWorldStr "<HTML><HEAD><TITLE>Hello</TITLE></HEAD><BODY><P>Hello, World!</P></BODY></HTML>"
-
- /* gHTTPHeaderOKSize is the size of the default OK HTTP header that you need
- to prepend to your data.
- The extra 1 at the end is for the string's null terminator. */
- (*theCGIHdl)->responseSize = gHTTPHeaderOKSize + strlen(kHelloWorldStr) + 1;
-
- /* allocate the responseData */
- (*theCGIHdl)->responseData = CGINewHandle ( theCGIHdl, (*theCGIHdl)->responseSize, nil );
- if ( (*theCGIHdl)->responseData != NULL )
- {
- /* lock theCGIHdl so we can work with its data not moving */
- HLock ( (*theCGIHdl)->responseData );
- /* get a direct pointer to the responseData to make things a little easier */
- myResponseData = *((*theCGIHdl)->responseData);
-
- /* copy the default HTTP OK header to the begining of our data */
- strcpy ( myResponseData, (char *)gHTTPHeaderOK );
- /* copy our data - "Hello, World!" */
- strcpy ( &(myResponseData[gHTTPHeaderOKSize]), kHelloWorldStr );
-
- /* don't leave theCGIHdl locked when done */
- HUnlock ( (*theCGIHdl)->responseData );
- }
- else
- {
- /* unable to allocate data - framework will return proper http error header */
- (*theCGIHdl)->responseSize = nil;
- }
-
- /* you should try to make use of giving time,
- especially in for and while loops or where you have to wait for
- some other process to return data or finish a task. */
- ProcessGiveTime ( nil, true, theCGIHdl );
- } /* CustomCGIProcess */
-
-
- /* this function will be called after the cgi result has been returned.
- It will contain the same CGIHdl that was used for the CustomCGIProcess.
- This function's prototype is defined in "CustomHandlers.h" */
- void
- CustomCGIPostProcess ( CGIHdl theCGIHdl )
- {
- /* as an example, I'll log some info here */
- LogStringBreakP ( "\pHere is a post-process log entry:" );
-
- #if kCompileWithCGIfull_request
- /* now I'll try to log the full_request, if it's present */
- if ( (*theCGIHdl)->full_request != NULL && ((*theCGIHdl)->full_request)[nil] != nil )
- {
- //••• LogStringP ( "\p\tfull_request: " );
- //••• LogStringBreak ( (*theCGIHdl)->full_request );
- }
- #endif
- } /* CustomCGIPostProcess */
-
-
- //••• this function has been removed
- //••• use 'CustomStartup' in "MyHandlers.c" instead.
- //
- ///*** CUSTOM CGI INITIALIZATION ***/
- //#pragma segment Startup
- //
- ///* Put any of the initialization you need done, here.
- // This function will be called once: in-between the startup
- // sequence and the main event loop.
- // Return true if the initialization was successful, otherwise false.
- // An initialization failure will result in the application quitting. */
- //Boolean
- //CustomCGIStartup ( void )
- //{
- // return true;
- //} /* CustomCGIStartup */
-
-
- //••• this function has been removed
- //••• use 'CustomQuit' in "MyHandlers.c" instead.
- //
- ///*** CUSTOM CGI CLEAN UP ***/
- //#pragma segment Main
- //
- ///* This function is called at quitting time. Put any cleanup you need to do in it.
- // Return true if you are succeful.
- // Return false if you are unable to quit for some reason (this generally only
- // applies when the user cancels)
- // allowUserInteract specifies whether you can make user interface calls.
- // However, CGIs generally should not rely on any user interface calls, so don't
- // depend on them. */
- //Boolean
- //CustomCGIQuit ( Boolean allowUserInteract )
- //{
- //#pragma unused (allowUserInteract)
- // return true;
- //}/* CustomCGIQuit */
-
-
- /*** WSAPI SUPPORT ***/
- #if kCompilingForWSAPI
-
- #define kFileCreatorTypeAny '* '
-
- #define kMyPluginAction "GRANTSCGI"
- #define kMyFileExtension ".GRANT"
-
- /* This function is called when the WSAPI_Register event is received.
- You can not do anything other than registering actions and suffixes,
- and identifying this plug-in's capabilities.
- See the WSAPI documentation [section 2.2.2] for further details. */
- WSAPI_ErrorCode
- CustomCGIWSAPIRegister ( WSAPI_CommandPBPtr commandPtr )
- {
- WSAPI_ErrorCode theErr;
-
- /* return plug-in name and abilities, register actions and suffixes. */
-
- /* your code must make at least one of the following WSAPI Register
- calls or there's no way for WebSTAR to pass CGI requests to your plug-in */
-
- /* in this example, we're registering "GRANTSCGI" as an action.
- You should always use the kMyCGIName constant (don't forget to properly
- set it in "MyConfiguration.h". */
- theErr = WSAPI_RegisterAction ( commandPtr, kMyPluginAction, kMyCGIName );
- if ( theErr != WSAPI_I_NoErr )
- {
- /* PlugIn Action didn't register. Notify user.
- May want to take some action other than just
- displaying this message */
- WSAPI_DisplayMessage ( commandPtr, kMyCGIName ": Couldn't register the action." );
- }
-
- /* in this example, we're registering ".GRANTSCGI" as a suffix mapping for
- the "GRANTSCGI" action (registered above). We're also registering it
- for files that have the TEXT filetype and CGI? as their creator type.
- If you don't care about particular file or creator types, then just
- use '* ' (defined as kFileCreatorTypeAny directly above this function). */
- theErr = WSAPI_RegisterSuffix ( commandPtr, kMyPluginAction, kMyFileExtension,
- kFileCreatorTypeAny, kFileCreatorTypeAny, "text/html" );
- //'TEXT', 'CGI?', "text/html" );
- if ( theErr != WSAPI_I_NoErr )
- {
- /* PlugIn Suffix didn't register. Notify user.
- May want to take some action other than just
- displaying this message */
- WSAPI_DisplayMessage ( commandPtr, kMyCGIName ": Couldn't register the suffix." );
- }
-
- /* you need to tell the server which capabilities you support. The required
- constants are defined in <WSAPI.h>. Add the appropriate ones together
- and store their sum in the capabilities field of the commandPtr.
- capCGI - able to perform the standard CGI role
- capPreProcessor - able to perform the preprocessor role
- capPostProcessor - able to perform the postprocessor role
- capLogging - can handle logging and status messages
- capSecurity - able to perform the security role */
- commandPtr->param.init.capabilities = capCGI;
-
- return theErr;
- } /* CustomCGIWSAPIRegister */
-
- #endif /* kCompilingForWSAPI */
-
-
- #endif /* kCompileWithCGICode */
-
- /*** EOF ***/
-